package Element.Geom;

import java.awt.*;
import java.awt.geom.*;
import Element.*;
import DistSys.*;

public class BusGeom extends TwoNodeElemGeom implements Cloneable {

    private boolean isVisited;   //flag for modifying the connectivity info.

    public BusGeom() {
        super();
        isVisited = false;
    }

    //init from drawing ; this is an *overloaded* method!!!
    public void initFromScreen(String n) {
        init(n);
    }

    //overridden methods
    public void calcFrame(){} // do nothing; frame is N/A to line

    public void toggle(){  }


    //**********************************************
    //overridden methods
    //**********************************************

    public boolean coverPoint( int x, int y, int snap){
        if( isStartPoint(x,y, snap) == true ) return true;
        if( isEndPoint(x,y, snap) == true) return true;

        Line2D.Float ln = new Line2D.Float();
        ln.setLine( x1, y1, x2, y2); // auto cast to 4 floats

        if( snap == 0) {
            if( ln.ptLineDist(x,y)<=0.05  &&  //point could be at the extension of the line
                ln.intersects( x-2, y-2, 4, 4)==true )
                return true;
            else return false;
            //return ln.contains(x,y); //snap ==0 if checking connectivity
        } else return ln.intersects(x-snap,y-snap, 2*snap, 2*snap);
    }//end of coverPoint()

    /*public void drawFrame(Graphics g){
      //the stroke for Bus is for BOLD line; no need to create a new stroke for Bus
      int d=SysConst.NODE_SIZE;
      g.setColor(Color.yellow);
      g.drawLine(x1,y1, x2,y2);
      g.setColor(Color.black);
    }*/

    public void incRotate(){} // do nothing; rotate is N/A to line


    /*====================================
    Bus 's own method
    ====================================*/
    public boolean getIsVisited(){ return isVisited;}
    public void setIsVisited(boolean b){ isVisited=b;}

    /**
     * based on Stored Value to find system connectivity
     */
    public boolean coverPointBasedonStored( int x, int y){
        if( x == x1Stored && y == y1Stored) return true;
        if( x == x2Stored && y == y2Stored) return true;

        Line2D.Float ln = new Line2D.Float();
        ln.setLine( x1Stored, y1Stored, x2Stored, y2Stored); // auto cast to 4 floats

        if( ln.ptLineDist(x,y)<=0.05  &&  //point could be at the extension of the line
            x >= Math.min(x1Stored, x2Stored) &&
            x <= Math.max(x1Stored, x2Stored) &&
            y >= Math.min(y1Stored, y2Stored) &&
            y <= Math.max(y1Stored, y2Stored) )
            return true;
        else return false;
    }//end of coverPointBasedonStored()


    /**
     * based on Stored Value
     */
    public boolean isConnectedWithBus( BusGeom bg, int snap ){// int x3, int y3, int x4, int y4 ){
        int x3 = bg.x1Stored;
        int y3 = bg.y1Stored;
        int x4 = bg.x2Stored;
        int y4 = bg.y2Stored;
        Line2D.Float ln = new Line2D.Float();
        ln.setLine( this.x1Stored, this.y1Stored, this.x2Stored, this.y2Stored); // auto cast to 4 floats
        if( ln.intersectsLine(x3,y3, x4,y4) ) return true;
        else if( this.coverPoint(x3,y3, snap) ) return true;
        else if( this.coverPoint(x4,y4, snap) ) return true;
        else return false;
    }

    //not a real implementation; real implementation is in the inner class of elements
    //public void draw(Graphics g){}

    public Object clone(){
        BusGeom created = new BusGeom(); // all ref. in created has been instantiated
                                         // such as frame, conn1,conn2, childList

        this.copyTo( created);// copy AbsElemGeom attributes to created

        //BusGeom's own data
        created.isVisited = this.isVisited;
        return created;
    }
}